package com.raymond.cmpp.common.netty;

import com.raymond.cmpp.common.codec.BaseCodecStrategy;
import com.raymond.cmpp.common.msg.BaseMessage;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 消息编码
 *
 * @author :  raymond
 * @version :  V1.0
 * @date :  2021-08-05 15:41
 */
public class MsgEncoder extends MessageToByteEncoder<BaseMessage> {

    private final static Logger logger = LoggerFactory.getLogger(MsgEncoder.class);

    @Override
    protected void encode(ChannelHandlerContext ctx, BaseMessage msg, ByteBuf out) {
        try {
            BaseCodecStrategy.getBaseEncoder(msg.getCommandId()).encode(ctx, msg, out);
        } catch (Exception e) {
            logger.error("消息编码异常,commandId:{}", msg.getCommandId(), e);
        }
    }

}
